<!--
 * 审核弹框
-->
<template>
  <el-dialog title="审核" :visible.sync="dialogVisible" width="30%" @opened="opened" @close="$emit('close')">
    <el-form :model="formInline" ref="giftProEdit" :rules="rules" :disabled="curType == 2" class="demo-form-inline">
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="formInline.auditStatus" placeholder="请选择">
          <el-option label="通过" :value="1"></el-option>
          <el-option label="不通过" :value="-1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核原因" prop="auditReason">
        <el-input type="textarea" style="width: 100%;" v-model="formInline.auditReason" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
      <el-button type="primary" @click="confirmEdit('giftProEdit')" size="mini" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { doAuditByUpOrg, batchDoAuditByUpOrg } from '@/api/stockAudit';
export default {
  name: 'AuditDialog',
  props: {
    show: { type: Boolean, default: false },
    detail: { type: Object, default: () => ({}) },
    // 1-编辑 2-查看
    curType: { type: [String, Number], default: 1 },
    // 出现多选时使用
    multipleIds: { type: Array, default: () => [] },
  },
  data() {
    return {
      formInline: {
        auditStatus: '',
        auditReason: '',
      },
      loading: false,
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    },
    rules() {
      if (this.formInline.auditStatus == 1) {
        return {
          auditStatus: [
            { required: true, message: '请选择审核状态', trigger: 'change' }
          ],
        }
      } else {
        return {
          auditStatus: [
            { required: true, message: '请选择审核状态', trigger: 'change' }
          ],
          auditReason: [
            { required: true, message: '请输入审核原因', trigger: 'blur' }
          ]
        }
      }
    }
  },
  methods: {
    opened() {
      this.$nextTick(() => {
        this.$refs.giftProEdit && this.$refs.giftProEdit.resetFields();
        const arr = ['auditStatus', 'auditReason'];
        arr.forEach(item => {
          this.formInline[item] = this.detail[item] || '';
        });
      })
    },
    confirmEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.detail.uuid) {
            this.loading = true
            doAuditByUpOrg({
              auditLogId: this.detail.uuid,
              auditStatus: this.formInline.auditStatus,
              auditReason: this.formInline.auditReason
            }).then(res => {
              if (res.code == 1) {
                this.$message.success('审核成功');
                this.dialogVisible = false;
                this.$emit('refresh');
              } else {
                this.$message.error(res.message || '审核失败');
              }
            }).finally(() => {
              this.loading = false;
            })
          } else if (this.multipleIds.length > 0) {
            this.loading = true
            batchDoAuditByUpOrg({
              auditLogIds: this.multipleIds,
              auditStatus: this.formInline.auditStatus,
              auditReason: this.formInline.auditReason
            }).then(res => {
              if (res.code == 1) {
                this.$message.success('审核成功');
                this.dialogVisible = false;
                this.$emit('refresh');
              } else {
                this.$message.error(res.message || '审核失败');
              }
            }).finally(() => {
              this.loading = false;
            })
          }
        }
      })
    }
  }
}
</script>